package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.CpkInfo;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.mapper.CpkInfoMapper;
import com.baomidou.ant.service.ICpkInfoService;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.vo.request.RequestGetAllCpkChartVo;
import com.baomidou.ant.vo.request.RequestGetCpkChartVo;
import com.baomidou.ant.vo.request.RequestSearchCpkVo;
import com.baomidou.ant.vo.response.ResponseAllCpkChartVo;
import com.baomidou.ant.vo.response.ResponseCpkChartVo;
import com.baomidou.ant.vo.response.ResponseListAllCpkVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@Service
public class CpkInfoServiceImpl extends ServiceImpl<CpkInfoMapper, CpkInfo> implements ICpkInfoService {
    @Autowired
    ISysUserService userService;

    @Override
    public CpkInfo queryByOrderNum(String orderNum) {
        QueryWrapper<CpkInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_num",orderNum);
        return getOne(queryWrapper);
    }

    @Override
    public ResponseCpkChartVo getChartData(RequestGetCpkChartVo vo) {
        QueryWrapper<CpkInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("machine_num",vo.getMachineNum());
        SysUser user=userService.queryByLoginName(vo.getLoginName());
        //判断管理员级别
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(ObjectUtils.isNotEmpty(vo.getDeptId())){
                queryWrapper.eq("dept_id",vo.getDeptId());
            }
        } else if(Objects.equals(user.getType(), "0")||Objects.equals(user.getType(), "2")){//只能看自己部门的
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        List<CpkInfo> cpkInfos=list(queryWrapper);
        List<String> orderNumList=new ArrayList<>();
        List<Double> cpkList=new ArrayList<>();
        for(CpkInfo cpkInfo:cpkInfos){
            //赋值
            if(ObjectUtils.isEmpty(cpkInfo.getOrderNum())){
                orderNumList.add("未知的LOT");
            }else
                orderNumList.add(cpkInfo.getOrderNum());
            if(ObjectUtils.isEmpty(cpkInfo.getCpk())){
                cpkList.add((double) 0);
            }else
                cpkList.add(cpkInfo.getCpk());
        }
        ResponseCpkChartVo responseCpkChartVo=new ResponseCpkChartVo();
        responseCpkChartVo.setCpkList(cpkList);
        responseCpkChartVo.setOrderNumList(orderNumList);
        return responseCpkChartVo;
    }

    @Override
    public ResponseAllCpkChartVo getAllChartData(RequestGetAllCpkChartVo vo) {
        QueryWrapper<CpkInfo> queryWrapper=new QueryWrapper<>();
        SysUser user=userService.queryByLoginName(vo.getLoginName());
        //判断管理员级别
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(ObjectUtils.isNotEmpty(vo.getDeptId())){
                queryWrapper.eq("dept_id",vo.getDeptId());
            }
        } else if(Objects.equals(user.getType(), "0")||Objects.equals(user.getType(), "2")){//只能看自己部门的
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        List<CpkInfo> cpkInfos=list(queryWrapper);
        List<String> orderNumList=new ArrayList<>();
        List<Double> cpkList=new ArrayList<>();
        List<String> machineNumList=new ArrayList<>();
        for(CpkInfo cpkInfo:cpkInfos){
            //赋值
            if(ObjectUtils.isEmpty(cpkInfo.getOrderNum())){
                orderNumList.add("未知的LOT");
            }else
                orderNumList.add(cpkInfo.getOrderNum());
            if(ObjectUtils.isEmpty(cpkInfo.getCpk())){
                cpkList.add((double) 0);
            }else
                cpkList.add(cpkInfo.getCpk());
            if(ObjectUtils.isEmpty(cpkInfo.getMachineNum())){
                machineNumList.add("未知的作业机台");
            }else
                machineNumList.add(cpkInfo.getMachineNum());
        }
        ResponseAllCpkChartVo responseAllCpkChartVo=new ResponseAllCpkChartVo();
        responseAllCpkChartVo.setCpkList(cpkList);
        List<String> numList=new ArrayList<>();
        for(int i=0;i<orderNumList.size();i++){
            String num=orderNumList.get(i);
            if(machineNumList.get(i)==null|| Objects.equals(machineNumList.get(i), "")){
                num=num+"(未知机台)";
            }else
                num=num+" ("+machineNumList.get(i)+")";
            numList.add(num);
        }
        responseAllCpkChartVo.setNumList(numList);
        return responseAllCpkChartVo;
    }

    @Override
    public IPage<ResponseListAllCpkVo> listAllCpk(RequestSearchCpkVo vo) {
        IPage<CpkInfo> iPage=new Page<>(vo.getDisplayStart(),vo.getDisplayLength());
        QueryWrapper<CpkInfo> queryWrapper=new QueryWrapper<>();
        SysUser user=userService.queryByLoginName(vo.getLoginName());
        //判断管理员级别
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(ObjectUtils.isNotEmpty(vo.getDeptId())){
                queryWrapper.eq("dept_id",vo.getDeptId());
            }
        } else if(Objects.equals(user.getType(), "0")||Objects.equals(user.getType(), "2")){//只能看自己部门的
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        IPage<CpkInfo> page=page(iPage,queryWrapper);
        return page.convert(cpkInfo -> {
            ResponseListAllCpkVo responseSearchCpkVo=new ResponseListAllCpkVo();
            BeanUtils.copyProperties(cpkInfo,responseSearchCpkVo);
            return responseSearchCpkVo;
        });
    }

}
